import { glSDKMgr } from "../../lib/GLSDKMgr";
import { GCtrl } from "../../lib/util/GCtrl";
import Screenshots from "../../lib/Screenshots/Screenshots";

const { ccclass, property } = cc._decorator;

@ccclass
export default class DWC_PersonalInfo extends cc.Component {

    @property(cc.Node)
    btnBangDing: cc.Node = null;
    @property(cc.Sprite)
    headIcon: cc.Sprite = null;
    @property(cc.Label)
    ridLb: cc.Label = null;
    @property(cc.Label)
    nameLb: cc.Label = null;
    @property(cc.Label)
    idLb: cc.Label = null;
    @property(cc.Label)
    coinLb: cc.Label = null;
    @property(cc.Label)
    phoneLb: cc.Label = null;

    // LIFE-CYCLE CALLBACKS:
    private myInfo = GCtrl.session.getPlayer();
    private eventIndexList: any = {};

    // onLoad () {}

    onEnable() {
        this.eventIndexList["changeHeadSuccess"] = GCtrl.eventCustom.on("changeHeadSuccess", this.changeHeadSuccess.bind(this), this.node);
        this.eventIndexList["bindPhoneSuccess"] = GCtrl.eventCustom.on("bindPhoneSuccess", this.bindPhoneSuccess.bind(this), this.node);
        this.eventIndexList["changeNickSuccess"] = GCtrl.eventCustom.on("changeNickSuccess", this.changeNickSuccess.bind(this), this.node);
        this.initView();
    }
    onDisable() {
        if (this.eventIndexList) {
            for (let _key in this.eventIndexList) {
                this.eventIndexList[_key] && GCtrl.eventCustom.off(_key, this.eventIndexList[_key]);
                delete this.eventIndexList[_key];
            }
        }
    }

    start() {

    }

    // update (dt) {}
    init(args) {
    }
    private initView() {
        this.myInfo = GCtrl.session.getPlayer();
        this.setNick(this.myInfo.rolename);
        this.idLb.string = this.myInfo.rid + "";
        this.coinLb.string = GCtrl.setDecimalNum((this.myInfo.coin / 100) + "", 0);
        this.phoneLb.string = this.myInfo.phone || "暂未绑定";
        this.ridLb.string = `我的邀请码: ${this.myInfo.rid}`;

        this.changeHeadSuccess([this.myInfo.logo]);
        this.btnBangDing.active = !this.myInfo.phone;
    }
    /** 设置昵称 */
    private setNick(_nick: string): void {
        this.nameLb.string = GCtrl.visibleStr(_nick, 5);
    }
    private changeHeadSuccess(data) {
        let headId = data[0];
        return GCtrl.setHeadSpr(headId, this.headIcon);
    }
    private bindPhoneSuccess(data) {
        let phone = data[0];
        this.phoneLb.string = this.myInfo.phone + "";
    }
    /** 昵称修改成功 */
    private changeNickSuccess(_evt): void {
        this.setNick(_evt[0]);
    }
    onClickBack() {
        GCtrl.UIMgr.close(GCtrl.GLConfig.DWC_PersonalInfo_path, GCtrl.UIMgr.closeType.hide);
        cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
    }
    onClickChangeHead() {
        GCtrl.UIMgr.open(GCtrl.GLConfig.DWC_ChangeHead_path);
        cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
    }
    onClickCopyId() {
        glSDKMgr.setClipboard(this.idLb.string);
        GCtrl.GLUtils.showTips("复制成功");
        cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
    }
    onClickBindPhone() {
        GCtrl.UIMgr.open(GCtrl.GLConfig.DWC_BindPhone_path);
        cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
    }
    onClickChangePW() {
        GCtrl.UIMgr.open(GCtrl.GLConfig.DWC_ChangePW_path);
        cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
    }
    /** 点击修改昵称 */
    onClickChangeNick(): void {
        GCtrl.UIMgr.open(GCtrl.GLConfig.DWC_ChangeNick_path);
        cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
    }
    onClickSend() {
        //xian检查是否绑定了手机号
        if (!this.myInfo.phone) {
            GCtrl.UIMgr.open(GCtrl.GLConfig.DWC_BindPhone_path);
            return;
        }

        GCtrl.UIMgr.open(GCtrl.GLConfig.DWC_ZengSong_path);
        cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
    }


    onClickShare() {

        if (cc.sys.isNative) {
            //分享截屏至大厅
            let screenShot_ts: Screenshots = new Screenshots();
            let path = screenShot_ts.saveImg(this.node, false);
            let reqContent =
            {
                type: '1',  //0 文字, 1 图片
                scene: '0', //0 好友, 1 朋友圈
                title: 'ATT皇冠电玩城',
                description: 'ATT皇冠电玩城',//简要描述
                content: path,//内容, 图片则是全路径
            }
            cc.vv.GLSDKMgr.wxShare(reqContent)
            cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
        }

    }
}
